home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / simula / books / books.lha / kirkerud / examset.sim < prev    next >
Text File  |  1993-08-16  |  16KB  |  507 lines

  1. % ****************************************************************
  2. % *                                                              *
  3. % *  This is a revised version of the program constructed in     *
  4. % *  section 14.7 of                                             *
  5. % *  Object Oriented Programming with Simula by Bj|rn Kirkerud;  *
  6. % *  The revisions in the program consist in the use of          *
  7. % *  a few classes containing generally useful tools             * 
  8. % *                                                              *
  9. % ****************************************************************
  10.  
  11.  
  12. external class Settools;
  13.  
  14. settools begin
  15.  
  16.  
  17. % ****************************************************************
  18. % *                                                              *
  19. % *  Declarations of classes containing generally usefull tools: *
  20. % *                                                              *
  21. % ****************************************************************
  22.  
  23.   external class promptools, texttools;
  24.  
  25.   ref(promptools) pt;
  26.   ref(texttools)  tt;
  27.  
  28.   procedure outline(line); text line;
  29.     begin outtext(line); outimage end;
  30.  
  31.  
  32.  
  33. % ****************************************************************
  34. % *                                                              *
  35. % *  The class Student:                                          *
  36. % *                                                              *
  37. % ****************************************************************
  38.  
  39. element class Student;
  40.   begin
  41.  
  42. %  Declarations of identifying data attribute: 
  43.  
  44.     text ident;
  45.  
  46.     text procedure key; 
  47.       key :- ident;
  48.  
  49.  
  50. %  Declarations of other data attributes: 
  51.  
  52.    ! Dropped here;
  53.  
  54.  
  55. %  Declarations of two sets:  
  56.  
  57.     ref(ordered_set) courses, ! Contains references to all courses followed 
  58.                               ! by this Student;
  59.                  exams;   ! Contains references to all exams taken by 
  60.                               ! this Student;
  61.  
  62.  
  63. %  Declarations of data access procedures:  
  64.  
  65.     procedure display;
  66.       begin ref(Course) c; ref(Exam) e;
  67.         outtext("  Student " & key & " follows ");
  68.         if courses.size = 0 
  69.           then outtext("no course.")
  70.           else begin
  71.             outint(courses.size, 0); outtext(" course(s): ");
  72.             c :- courses.first_element;
  73.             while c =/= none do
  74.               begin outtext(c.key & ", ");  c :- courses.next_element end;
  75.           end;
  76.         outimage;
  77.         outtext("    He/she has exams in ");
  78.         if exams.size = 0 
  79.           then outtext("no courses.")
  80.           else begin
  81.             outtext("the following "); outint(exams.size, 0); 
  82.             outtext(" course(s): ");
  83.             e :- exams.first_element;
  84.             while e =/= none do
  85.               begin
  86.                 outimage;
  87.                 outtext("      " & e.course_code & ", grade: "  & e.grade);
  88.                 e :- exams.next_element;
  89.               end;
  90.           end;
  91.         outimage;
  92.       end of display;
  93.  
  94.     procedure read;
  95.       begin 
  96.         ! Imperatives that prompt for and read data-attributes
  97.         ! of this Student.  No such imperatives are necesseray
  98.         ! in this version because all data-attributes (except ident) 
  99.         ! have been dropped;
  100.       end;
  101.  
  102.     procedure put_to_file(stud_file);  ref(outfile) stud_file;
  103.     inspect stud_file do
  104.       begin outtext(ident); outimage end;
  105.  
  106.     procedure get_from_file(stud_file);  ref(infile) stud_file;
  107.     inspect stud_file do
  108.       begin inimage; ident :- copy(image.strip) end;
  109.  
  110. %  Initialization of the data structure of this Student:   
  111.  
  112.     courses :- new ordered_set;
  113.     exams   :- new ordered_set;
  114.  
  115.   end of Student;
  116.  
  117.  
  118. element class Course;
  119.   begin
  120.  
  121. %  Declarations of identifying data attribute:   
  122.  
  123.     text code;
  124.  
  125.     text procedure key; 
  126.       key :- code;
  127.  
  128.  
  129. %  Declarations of other data attributes:   
  130.  
  131.     ! Dropped here;
  132.  
  133.  
  134. %  Declarations of two sets:   
  135.  
  136.     ref(ordered_set) students,  ! Contains references to all students who 
  137.                                 ! follow this Course;
  138.                  exams;     ! Contains references to all exams taken 
  139.                                 ! in this Course;
  140.  
  141. %  Declarations of data access procedures: 
  142.  
  143.     procedure display;
  144.       begin ref(Student) s;
  145.         outtext("  Course " & key & " is followed by ");
  146.         if students.size = 0 then outtext("no students.")
  147.         else begin
  148.             outint(students.size, 0); outtext(" student(s): ");
  149.             s :- students.first_element;
  150.             while s =/= none do
  151.               begin outtext(s.key & ", ");  s :- students.next_element end;
  152.           end;
  153.         outimage;
  154.       end of display;
  155.  
  156.     procedure read;
  157.       begin ! Not necessary in this simplified version; end;
  158.  
  159.     procedure put_to_file(stud_file);  ref(outfile) stud_file;
  160.     inspect stud_file do
  161.       begin ref(Student) s; ref(Exam) e;
  162.         outtext(code); outimage;        
  163.         outint(students.size, 0); outimage;
  164.         s :- students.first_element;
  165.         while s =/= none do
  166.           begin
  167.             outtext(s.ident); outimage;
  168.             s :- students.next_element;
  169.           end;
  170.         outint(exams.size, 0); outimage;
  171.         e :- exams.first_element;
  172.         while e =/= none do
  173.           begin
  174.             e.put_to_file(stud_file);
  175.             e :- exams.next_element;
  176.           end;
  177.       end;
  178.  
  179.     procedure get_from_file(stud_file);  ref(infile) stud_file;
  180.     inspect stud_file do
  181.       begin 
  182.         ref(Student) s; ref(Exam) e;
  183.         integer nstudents, studentn, nexams, examn;
  184.         inimage; code      :- copy(image.strip);
  185.         inimage; nstudents := inint;
  186.         for studentn := 1 step 1 until nstudents do
  187.           begin
  188.             inimage; 
  189.             s :- all_students.find_element(copy(image.strip));
  190.             if s =/= none then
  191.               begin
  192.                 students.add_element_ok(s);
  193.                 s.courses.add_element_ok(this Course);
  194.               end;
  195.           end;
  196.         inimage; nexams := inint;
  197.         for examn := 1 step 1 until nexams do
  198.           begin
  199.             e :- new Exam; e.get_from_file(this infile);
  200.             all_exams.add_element_ok(e);
  201.             exams.add_element_ok(e);
  202.             s :- all_students.find_element(e.student_ident);
  203.             if s =/= none then s.exams.add_element_ok(e);
  204.           end;
  205.       end;
  206.  
  207.  
  208. % Initialization of the local data structure of this Course:   ;
  209.  
  210.     students :- new ordered_set;
  211.     exams    :- new ordered_set;
  212.  
  213.   end of Course;
  214.  
  215.  
  216. element class Exam;
  217.   begin
  218.  
  219.     text course_code, student_ident, grade;
  220.  
  221.     text procedure key; key :- course_code &  "!127!" & student_ident;
  222.  
  223.     procedure display;
  224.       begin
  225.         outtext("  Exam in course " & course_code & 
  226.                 " by student " &  student_ident &
  227.             ", grade: " & grade);
  228.         outimage;
  229.       end;
  230.  
  231.     procedure put_to_file(stud_file);  ref(outfile) stud_file;
  232.     inspect stud_file do
  233.       begin 
  234.         outtext(course_code);   outimage;
  235.         outtext(student_ident); outimage;
  236.         outtext(grade);         outimage;
  237.       end;
  238.  
  239.     procedure get_from_file(stud_file);  ref(infile) stud_file;
  240.     inspect stud_file do
  241.       begin 
  242.         inimage; course_code   :- copy(image.strip);
  243.         inimage; student_ident :- copy(image.strip);
  244.         inimage; grade         :- copy(image.strip);
  245.       end;
  246.  
  247.   end of Exam;
  248.  
  249.  
  250.  
  251. ref(ordered_set) all_students, !  A set of all students;
  252.          all_courses,  !  A set of all courses;
  253.          all_exams;    !  A set of all exams;
  254.  
  255.  
  256. % Declarations of command procedures:  ;
  257.  
  258. procedure Give_help;
  259.   begin
  260.     outline("The legal commands are: ");
  261.     outline("   ?:   Help (writes this text)");
  262.     outline("   s:   To enter data about a new student");
  263.     outline("   c:   To enter data about a new course");
  264.     outline("   j:   Student joins a course");
  265.     outline("   sq:  Student quits a course");
  266.     outline("   e:   Student takes exam in a course");
  267.     outline("   ds:  Displays data about a specified student");
  268.     outline("   dc:  Displays data about a specified course");
  269.     outline("   das: Displays all students");
  270.     outline("   dac: Displays all courses");
  271.     outline("   dae: Displays all exams");
  272.     outline("   pf:  Puts all data to a file");
  273.     outline("   gf:  Gets all data from a file");
  274.     outline("   q:   Quit (the program execution stops)");
  275.   end of Give_help;
  276.  
  277. procedure Enter_student;
  278.   begin ref(Student) a_student;
  279.     a_student :- new Student;
  280.     a_student.ident :- pt.ask_for_text("Student ident? ");
  281.     if all_students.add_element_ok(a_student)
  282.       then a_student.read
  283.       else outline("This student has been entered before!");
  284.   end of Enter_student;
  285.  
  286. procedure Enter_course;
  287.   begin ref(Course) a_course; 
  288.     a_course :- new Course;
  289.     a_course.code :- pt.ask_for_text("Course code? ");
  290.     if all_courses.add_element_ok(a_course)
  291.       then a_course.read
  292.       else outline("This course has been entered before!");
  293.   end of Enter_course;
  294.  
  295. procedure Join_course;
  296.   begin
  297.     ref(Student) s; ref(Course) c; 
  298.     s :- all_students.find_element(pt.ask_for_text( "Student ident? "));
  299.     if s == none 
  300.       then outline( "No student with that ident!" )
  301.       else begin
  302.         c :- all_courses.find_element(pt.ask_for_text( "Course code? "));
  303.         if c == none
  304.           then outline( "No course with that code!")
  305.           else begin
  306.             s.courses.add_element_ok(c);
  307.             c.students.add_element_ok(s);
  308.             outline("Student " & s.key  & " has been accepted in course "
  309.                      & c.key);
  310.           end;
  311.       end;
  312.   end of Join_course;
  313.  
  314. procedure Quit_course;
  315.   begin
  316.     ref(Course) c; ref(Student) s;
  317.     s :- all_students.find_element(pt.ask_for_text( "Student ident? "));
  318.     if s == none 
  319.       then outline( "No student with that ident!" )
  320.       else begin
  321.         c :- all_courses.find_element(pt.ask_for_text( "Course code? "));
  322.         if c == none 
  323.           then outline( "No course with that code!")
  324.           else begin
  325.             if s.courses.remove_element_ok(c.key)
  326.               then begin
  327.                   c.students.remove_element_ok(s.key);
  328.                   outline("Student " & s.key &  " no longer follows course "
  329.                                    & c.key);
  330.                 end
  331.               else outline("The student did not follow that course!");
  332.           end of c =/= none;
  333.       end of s =/= none;
  334.   end of Quit_course;
  335.  
  336. procedure Take_exam;
  337.   begin
  338.     ref(Student) s; ref(Course) c; ref(Exam) e;
  339.     c :- all_courses.find_element(pt.ask_for_text( "Course code? "));
  340.     if c == none 
  341.       then outline( "No course with that code")
  342.       else begin
  343.         s :- all_students.find_element(pt.ask_for_text( "Student ident? "));
  344.         if s == none 
  345.           then outline( "No student with that ident!")
  346.           else begin
  347.             e :- new Exam;
  348.             e.course_code :- c.code;  e.student_ident :- s.ident;
  349.             e.grade :- pt.ask_for_text("Grade? ");
  350.             s.exams.add_element_ok(e);
  351.             c.exams.add_element_ok(e);
  352.             all_exams.add_element_ok(e);
  353.             s.courses.remove_element_ok(c.key);
  354.             c.students.remove_element_ok(s.key);
  355.             outline("Student " & s.key & " is recorded to have taken " &
  356.                      "exam in course " & c.key);
  357.           end of s =/= none;
  358.       end of c =/= none;
  359.   end of Take_exam;
  360.  
  361. procedure Display_student;
  362.   begin ref(Student) s;
  363.     s :- all_students.find_element(pt.ask_for_text( "Student ident? "));
  364.     if s == none 
  365.       then outline( "No student with that ident!")
  366.       else s.display;
  367.     outimage;
  368.   end;
  369.  
  370. procedure Display_course;
  371.   begin ref(Course) c;
  372.     c :- all_courses.find_element(pt.ask_for_text( "Course ident? "));
  373.     if c == none 
  374.       then outline( "No course with that ident!")
  375.       else c.display;
  376.     outimage;
  377.   end;
  378.  
  379. procedure Display_all_students;
  380.   begin ref(Student) s;
  381.     outline("The students:");
  382.     s :- all_students.first_element;
  383.     while s =/= none do 
  384.       begin s.display; s :- all_students.next_element end;
  385.     outimage;
  386.   end;
  387.  
  388. procedure Display_all_courses;
  389.   begin ref(Course) c;
  390.     outline("The courses:");
  391.     c :- all_courses.first_element;
  392.     while c =/= none do 
  393.       begin c.display; c :- all_courses.next_element end;
  394.     outimage;
  395.   end;
  396.  
  397. procedure Display_all_exams;
  398.   begin ref(Exam) e;
  399.     outline("The exams:");
  400.     e :- all_exams.first_element;
  401.     while e =/= none do 
  402.       begin e.display; e :- all_exams.next_element end;
  403.     outimage;
  404.   end;
  405.  
  406. procedure Put_to_file;
  407. inspect new outfile(pt.ask_for_text("File name? ")) do
  408.   begin 
  409.     ref(Student) s; ref(Course) c; ref(Exam) e;    
  410.     open(blanks(100));
  411.     outint(all_students.size, 0); outimage;
  412.     s :- all_students.first_element;
  413.     while s =/= none do
  414.       begin s.put_to_file(this outfile); s :- all_students.next_element end;
  415.     outint(all_courses.size, 0); outimage;
  416.     c :- all_courses.first_element;
  417.     while c =/= none do
  418.       begin c.put_to_file(this outfile); c :- all_courses.next_element end;
  419.     close;
  420.   end Put_to_file;
  421.  
  422. procedure Get_from_file;  
  423. inspect new infile(pt.ask_for_text("File name? ")) do
  424.   begin 
  425.     ref(Student) s; ref(Course) c; ref(Exam) e;
  426.     integer nstudents, studentn, ncourses, coursen;  
  427.     if open(blanks(100))
  428.       then begin
  429.         inimage; nstudents := inint;
  430.         for studentn := 1 step 1 until nstudents do
  431.           begin
  432.             s :- new student;
  433.             s.get_from_file(this infile);
  434.             all_students.add_element_ok(s);
  435.           end;
  436.         inimage; ncourses := inint;
  437.         for coursen := 1 step 1 until ncourses do
  438.           begin
  439.             c :- new course;
  440.             c.get_from_file(this infile);
  441.             all_courses.add_element_ok(c);
  442.           end;
  443.         close;
  444.       end
  445.       else outline("The file & filename & can not be opened for reading!");
  446.   end Get_from_file;
  447.  
  448.  
  449. procedure Read_and_execute_commands;
  450.   begin text command;
  451.     Give_help;
  452.     command :- pt.ask_for_text("Enter command> ");
  453.     while command ne "q" do
  454.       begin
  455.         if command = "?"   then Give_help            else
  456.         if command = "s"   then Enter_student        else
  457.         if command = "c"   then Enter_course         else
  458.         if command = "j"   then Join_course          else
  459.         if command = "sq"  then Quit_course          else
  460.         if command = "e"   then Take_exam            else
  461.         if command = "ds"  then Display_student      else
  462.         if command = "dc"  then Display_course       else
  463.         if command = "das" then Display_all_students else
  464.         if command = "dac" then Display_all_courses  else
  465.         if command = "dae" then Display_all_exams    else
  466.         if command = "pf"  then Put_to_file          else
  467.         if command = "gf"  then Get_from_file
  468.         else begin
  469.             outline("Unknown command: " & command);
  470.             Give_help;
  471.           end;
  472.         command :- pt.ask_for_text("Enter command> ");
  473.       end;
  474.   end of Read_and_execute_commands;
  475.  
  476.  
  477. procedure Unknown_command(c); character c;
  478.   begin
  479.     outline("   You gave the command '" & tt.char_as_text(c) & "'" &
  480.             "   This is not among the legal commands.");
  481.     outline("   Type ? if you don't remember the legal commands"); 
  482.   end of Unknown_command;
  483.  
  484. procedure not_implemented(action_name);  text action_name;
  485.   begin
  486.     outline("   You gave the command """ & action_name &
  487.             """.  This has not been implemented yet!");
  488.     outline("   Try another command!"); 
  489.   end;
  490.  
  491. pt :- new promptools;
  492. tt :- new texttools;
  493.  
  494. all_students :- new ordered_set;
  495. all_courses  :- new ordered_set;
  496. all_exams    :- new ordered_set;
  497.  
  498. Read_and_execute_commands;
  499.  
  500. if pt.ask_for_bool("Do you want to put all data to file? ")
  501.   then Put_to_file;
  502.  
  503. outline("Bye");
  504.  
  505. end
  506.  
  507.